package com.kaishengit.util.web;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Employee;

public class LoginFilter extends AbstractFilter{

	String[] s;
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		
		String url = req.getRequestURI();
		for (int i = 0; i < s.length; i++) {
			if(s[i].equals(url)) {
				chain.doFilter(req, resp);
				return;
			}
		}
		HttpSession session = req.getSession();
		Employee e = (Employee) session.getAttribute("employee");
		if(e == null){
			ServletUtil.redirect(resp, "index.jspx?state=111");
		} else {
			chain.doFilter(req, resp);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		String str = filterConfig.getInitParameter("exclude");
		s = str.split(",");
	}
}
